package com.cn.elite.nj;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URLDecoder;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.upload.FormFile;

import com.cn.sx.form1;

public class action2 extends NonSessionAction{
	private static final Log log=LogFactory.getLog(action2.class);
	private com.cn.elite.nj.apbean apbean;




	public com.cn.elite.nj.apbean getApbean() {
		return apbean;
	}




	public void setApbean(com.cn.elite.nj.apbean apbean) {
		this.apbean = apbean;
	}




	public ActionForward getfile(ActionMapping mapping,
            ActionForm form,
            HttpServletRequest request,
            HttpServletResponse response) throws Exception{
		request.setCharacterEncoding("UTF-8");
		InputStreamReader input=null;
    	String fileURL=request.getParameter("path"); 
    	fileURL=URLDecoder.decode(fileURL,"UTF-8");
    	log.info("获得文件名:"+fileURL);
    	form2 form2=(form2)form;
    	FormFile file = form2.getTheFile(); //取得上传的文件
    	InputStream stream = file.getInputStream(); //把文件读入
    	input=apbean.decrypt(apbean.getArrayType()[2],stream);
    	String tablename=apbean.gettableName();
    	String sql1=apbean.getsql1(apbean.getArrayType1(),apbean.getArrayType2(),tablename,apbean.getdatebaseType());
    	if(sql1.equals("fail")){
    		request.setAttribute("result", "前置SQL语句获取错误");
    		return mapping.findForward("fail");
    	}
    	String[] updatesql=apbean.getupdatesql(input, sql1,apbean.getArrayType2());
    	int[] result1=apbean.update(updatesql);
    	int count=apbean.getfile(result1);
    	log.info("导入失败记录数:"+count);
    	log.info("-----------------------开始调用存储过程---------------------");
    	String result=apbean.run_excute();
    	String[] a=result.split("&&");
    	int cb_success=Integer.parseInt(a[0]);
    	int cb_fail=Integer.parseInt(a[1]);
    	
    	String reuqest1="";
    	reuqest1=reuqest1+"导入失败数:<font color='red'>"+count+"</font><br>";
    	reuqest1=reuqest1+"调用存储过程失败数:<font color='red'>"+cb_fail+"</font><br>";
    	reuqest1=reuqest1+"调用存储过程成功数:<font color='red'>"+cb_success+"</font><br>";
    	reuqest1=reuqest1+"总失败数:<font color='red'>"+(count+cb_fail)+"</font><br>";
    	reuqest1=reuqest1+"总成功数:<font color='red'>"+cb_success+"</font><br>";
    	

//    	apbean.execute1();
    	request.setAttribute("result",reuqest1);   	
        return mapping.findForward("success");
    	
    }

}
